package com.css.fxfzxczh.modules.disasterSituationDataManage.baseDataManage.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.css.fxfzxczh.modules.disasterSituationDataManage.baseDataManage.param.QueryByVersionParam;
import com.css.fxfzxczh.modules.disasterSituationDataManage.baseDataManage.param.SysDataVersionPageParam;
import com.css.fxfzxczh.modules.disasterSituationDataManage.baseDataManage.service.BasDatManageService;
import com.css.fxfzxczh.modules.disasterSituationDataManage.baseDataManage.vo.SysDataVersionPageVo;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.slw.common.helper.PropsHelper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author:
 * @CreateTime: 2023/8/2 14:20
 * @Description:
 */
@Service
public class BasDatManageServiceImpl implements BasDatManageService {

    @Resource(name = "httpsRestTemplates")
    RestTemplate restTemplate;

    @Override
    public List<Map<String, Object>> getMenuList() {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("sjxtRestUrl") + "/test/dictItems/getDataTypeLevel2AndDataTypeLevel3Tree";
        JSONObject valueMap = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "fxfzxczh");
        headers.set("subsystemCode", "dzxczhsspgzxt");
        HttpEntity<JSONObject> r = new HttpEntity<>(valueMap, headers);
        JSONArray data = restTemplate.exchange(url, HttpMethod.GET, r, JSONObject.class).getBody().getJSONArray("data");
        List<Map<String, Object>> list = new ArrayList<>();
        if(data.isEmpty()){
            return  list;
        }
        for (int i = 0; i < data.size(); i++) {
            JSONObject jsonObject = data.getJSONObject(i); // 获取每个元素的 JSONObject 对象
            Map<String, Object> map = jsonObject.toJavaObject(Map.class); // 将 JSONObject 转换为 Map
            list.add(map); // 将 Map 添加到 List 中
        }
        return list;

    }

    @Override
    public List<SysDataVersionPageVo> getVersionList(SysDataVersionPageParam param) {
        StringBuilder queryStr=new StringBuilder();
        if(PlatformObjectUtils.isNotEmpty(param.getDataRangeCountry())){
            queryStr.append("&dataRangeCountry="+param.getDataRangeCountry()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getDataRangeProvince())){
            queryStr.append("&dataRangeProvince="+param.getDataRangeProvince()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getDataRangeCity())){
            queryStr.append("&dataRangeCity="+param.getDataRangeCity()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getDataRangeArea())){
            queryStr.append("&dataRangeArea="+param.getDataRangeArea()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getDataType2())){
            queryStr.append("&dataType2="+param.getDataType2()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getVersionCode())){
            queryStr.append("&versionCode="+param.getVersionCode()+"");
        }


        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("sjxtRestUrl") + "/datacenter/sysDataVersion/list";
        JSONObject valueMap = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "fxfzxczh");
        headers.set("subsystemCode", "dzxczhsspgzxt");
        HttpEntity<JSONObject> r = new HttpEntity<>(valueMap, headers);
        String url1 = new StringBuilder(url).append("?").append(queryStr).toString();
        JSONArray data = restTemplate.exchange(url1, HttpMethod.GET, r, JSONObject.class).getBody().getJSONArray("data");
        List<SysDataVersionPageVo> list=new ArrayList<>();
        if(data.isEmpty()){
            return  list;
        }
        list = JSON.parseArray(data.toJSONString(), SysDataVersionPageVo.class);

        return list;
    }

    @Override
    public Map<String, Object> getListAndGeom(QueryByVersionParam param,int curPage,int pageSize) {
        StringBuilder queryStr=new StringBuilder();
        if(PlatformObjectUtils.isNotEmpty(param.getVersionCode())){
            queryStr.append("&versionCode="+param.getVersionCode()+"");
        }
        if(PlatformObjectUtils.isNotEmpty(param.getTypeCode())){
            queryStr.append("&typeCode="+param.getTypeCode()+"");
        }

        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("sjxtRestUrl") + "/datacenter/sysDataVersion/queryDataByVersionCode?page="+curPage+"&rows="+pageSize+"";
        JSONObject valueMap = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "fxfzxczh");
        headers.set("subsystemCode", "dzxczhsspgzxt");
        HttpEntity<JSONObject> r = new HttpEntity<>(valueMap, headers);
        String url1 = new StringBuilder(url).append(queryStr).toString();
        JSONObject data = restTemplate.exchange(url1, HttpMethod.GET, r, JSONObject.class).getBody().getJSONObject("data");
        String rows = data.getString("data");
        List<Map> list = JSON.parseObject(rows, new TypeReference<List<Map>>() {});
        Integer total = Integer.valueOf(data.getString("totalPage"));
        String title = data.getString("title");
        List<Map> titleList = JSON.parseObject(title, new TypeReference<List<Map>>() {});
        Map<String, Object> page = new HashMap<String, Object>();
        page.put("total", total);
        page.put("rows", list);
        page.put("title", titleList);
        return page;
    }
}
